/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.entity;

import horizon.component.CollisionComponent;
import horizon.component.Component;
import horizon.component.IRenderUpdate;
import horizon.component.RenderComponent;
import horizon.graphics.RenderingContext;
import horizon.util.Vector2;
import java.util.ArrayList;

/**
 *
 * @author J_Laptop
 */
public class Entity implements IRenderUpdate {
    
    public float speed;
    String id;
    public Vector2 position;
    float scale;
    float rotation;
    Vector2 movement = new Vector2(0);
    RenderComponent renderComponent = null;
    ArrayList<Component> components = null;
    
    public Entity(String id) {
        
        this.id = id;
        components = new ArrayList<Component>();
        
        position = new Vector2(0, 0);
        scale = 1;
        rotation = 0;
        
    }
    
    public void move(Vector2 v) {
        
        CollisionComponent d = (CollisionComponent) getComponent(CollisionComponent.class);
        
        if (d != null) {
            
            d.lastPos = position;
            
        }
        
        v = v.mult(speed / 100);
        
        movement = v;
        
        
        
    }
    
    public void move(long elapsedTime, Vector2 v) {
        
        position = position.add(v.mult((float) elapsedTime));
        movement = new Vector2(0);
        
    }
    
    public void rotate(float d) {
        
        
        rotation += d;
        
    }
    
    public void update(long elapsedTime) {
        move(elapsedTime, movement);
        
        for (Component component : components) {
            component.update(elapsedTime);
        }
    }
    
    public void render(RenderingContext r) {
        if (renderComponent != null) {
            renderComponent.render(r);
        }
    }
    
    public void AddComponent(Component component) {
        
        if (RenderComponent.class.isInstance(component)) {
            renderComponent = (RenderComponent) component;
        }
        
        component.setOwner(this);
        components.add(component);
        
    }
    
    public Component getComponent(Class id) {
        for (Component comp : components) {
            if (id.isInstance(comp)) {
                return comp;
            }
        }
        
        return null;
    }
    
    public Component getComponent(String id) {
        for (Component comp : components) {
            if (comp.getId().equalsIgnoreCase(id)) {
                return comp;
            }
        }
        
        return null;
    }
    
    public Vector2 getPosition() {
        return position;
    }
    
    public float getScale() {
        return scale;
    }
    
    public float getRotation() {
        return rotation;
    }
    
    public String getId() {
        return id;
    }
    
    public void setPosition(Vector2 position) {
        this.position = position;
    }
    
    public void setRotation(float rotate) {
        rotation = rotate;
    }
    
    public void setScale(float scale) {
        this.scale = scale;
    }
}
